
// 地址列表数据
var listdata = [
    {id:"1",realname: '张三', phone: "15026545678", isDefault: true, address: "北京 北京 东城区北京"},
    {id:"2",realname: '李四', phone: "15026545679", isDefault: false, address: "北京 北京 西城区北京"},
    {id:"3",realname: '王五', phone: "15026545610", isDefault: false, address: "上海 上海 西城区北京"},
    {id:"4",realname: '王五', phone: "15026545610", isDefault: false, address: "上海 上海 西城区北京"},
    {id:"5",realname: '王五', phone: "15026545610", isDefault: false, address: "上海 上海 西城区北京"},
    {id:"6",realname: '王五', phone: "15026545610", isDefault: false, address: "上海 上海 西城区北京"},
    {id:"7",realname: '王五', phone: "15026545610", isDefault: false, address: "上海 上海 西城区北京"},
    {id:"8",realname: '王五', phone: "15026545610", isDefault: false, address: "上海 上海 西城区北京"},
    {id:"9",realname: '王五', phone: "15026545610", isDefault: false, address: "上海 上海 西城区北京"},
    {id:"10",realname: '王五', phone: "15026545610", isDefault: false, address: "上海 上海 西城区北京"},
    {id:"11",realname: '王五', phone: "15026545610", isDefault: false, address: "上海 上海 西城区北京"},
    {id:"12",realname: '王五', phone: "15026545610", isDefault: false, address: "上海 上海 西城区北京"},
    {id:"13",realname: '王五', phone: "15026545610", isDefault: false, address: "上海 上海 西城区北京"},
]

var vm = new Vue({
    el: '#address',
    data: {
        items: listdata
    },
    methods:{
        insert:function (data) {
            // 注入数据
            vm.items = data
        },
        linktoCreate(){
            // 新增地址
            location.href="address_add.html";
        },
        setDefault(item){
            // 设置默认地址
            console.log('item',item);
            var adressid = item.id;
            var checked = item.isDefault;
            if(checked == false){
                mui.alert("设置 adressid =" + adressid);
                //异步操作
                setTimeout(function () {
                    vm.items.forEach((item, index) => {
                        item.isDefault = false;
                        if (item.id == adressid) {
                            item.isDefault = true;
                        }
                    })
                }, 1000)
            }
        },
        select(item){
            console.log('item',JSON.stringify(item))
            var $state = localStorage.getItem('$state')
            if($state){
                $state = JSON.parse($state);
                if($state.doSelect == 'true'){
                    localStorage.setItem('selected',JSON.stringify(item));
                    location.href = "../../pages/machine/machine_confirm.html"
                }
            }
        }
    }
});


mui.ready(function () {

    // 异步数据
    // vm.insert([]);

    // 删除地址
    mui('.mui-popover-action').on('tap', 'li>a', function() {
        var a = this,parent;
        //根据点击按钮，反推当前是哪个actionsheet
        for (parent = a.parentNode; parent != document.body; parent = parent.parentNode) {
            if (parent.classList.contains('mui-popover-action')) {
                break;
            }
        }
        //关闭actionsheet
        console.log('parent.id',parent.id)
        console.log('a.id',a.id)
        mui('#' + parent.id).popover('toggle');
        if (a.id == 'delete_item') {
            mui.alert("执行删除");
        }

    })

})